package com.jblv.frame.config.utils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title ConfigBootstrapProperties
 * @projectName jblv-frame
 * @packageName com.jblv.frame.config.utils
 * @description TODO
 * @date Create at 2020/9/7 17:08
 * @since jdk1.8
 */
public class ConfigBootstrapProperties {
    private final static Logger logger = LoggerFactory.getLogger(ConfigBootstrapProperties.class);

    private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/config/";

    private static final String DEFAULT_NAMES = "bootstrap.properties";

    private static final String JBLV_CONFIG_URI = "jblv.config.uri";

    private static final String JBLV_APPLICATION_HOST = "jblv.application.host";

    public static Properties properties = null;

    static {
        ConfigBootstrapProperties.properties = null;
        String home = System.getProperty("catalina.home");
        if (StringUtils.isBlank(home)) {
            home = ".";
        }
        final String[] locations = String.format(DEFAULT_SEARCH_LOCATIONS, home, home).split(",");
        Resource resource = null;
        InputStream is = null;
        for (final String location : locations) {
            try {
                final ResourceLoader resourceLoader = new DefaultResourceLoader();
                resource = resourceLoader.getResource(location + DEFAULT_NAMES);
                is = resource.getInputStream();
                logger.info(location + DEFAULT_NAMES);
                (ConfigBootstrapProperties.properties = new Properties()).load(is);
            }
            catch (FileNotFoundException e) {
                logger.debug(e.getMessage(), e);
            }
            catch (Exception e2) {
                logger.error(e2.getMessage(), e2);
            }
            finally {
                if (is != null) {
                    try {
                        is.close();
                    }
                    catch (IOException e3) {
                        logger.error(e3.getMessage(), e3);
                    }
                }
            }
        }
        if (resource == null) {
            logger.error("bootstrap.properties can't be found.");
        }
    }

    public static String getProperty(String name) {
        return properties.getProperty(name);
    }

    public static String getProperty(String name, String defaultValue) {
        String value = properties.getProperty(name);
        if (value == null) {
            return defaultValue;
        }
        return value;
    }

    public static String getUri() {
        return properties.getProperty(JBLV_CONFIG_URI);
    }

    public static String getHost() {
        String host = System.getProperty("jblv.host");
        if (host != null) {
            return host;
        }
        return properties.getProperty(JBLV_APPLICATION_HOST);
    }

}
